var qqmapsdk, app = getApp(), QQMapWX = require("../../utils/qqmap-wx-jssdk.js"), util = require("../../utils/util.js");

Page({
    data: {
        open: !1,
        sqlq: !1
    },
    tradeinfo: function(t) {
        console.log(t.currentTarget.dataset.id);
        var e = t.currentTarget.dataset.id, a = this.data.unreceive, o = this.data.received;
        console.log(e);
        for (var n = 0, s = a.length; n < s; ++n) a[n].id == e ? a[n].open = !a[n].open : a[n].open = !1;
        for (var n = 0, s = o.length; n < s; ++n) o[n].id == e ? o[n].open = !o[n].open : o[n].open = !1;
        this.setData({
            unreceive: a,
            received: o
        });
    },
    sqlq: function() {
        console.log("sqlq");
        var t = this;
        this.setData({
            sqlq: !t.data.sqlq
        });
    },
    maketel: function(t) {
        console.log(t.currentTarget.dataset.tel), wx.makePhoneCall({
            phoneNumber: t.currentTarget.dataset.tel
        });
    },
    ckwz: function(t) {
        console.log(t.currentTarget.dataset.jwd);
        var e = t.currentTarget.dataset.jwd.split(",");
        console.log(e);
        var a = this;
        wx.openLocation({
            latitude: Number(e[0]),
            longitude: Number(e[1]),
            name: a.data.mdinfo.name,
            address: a.data.mdinfo.address
        });
    },
    qhmd: function() {
        wx.navigateTo({
            url: "qhmd"
        });
    },
    md: function() {
      console.log(this.data);
      var a = this, e = getApp().imgurl, o = wx.getStorageSync("mdid");
      console.log(o);
      var i = wx.getStorageSync("UserData").id;
      console.log(i), a.lqyhq(i, o), app.util.request({
        url: "entry/wxapp/UserInfo",
        cachetime: "0",
        data: {
          user_id: i
        },
        success: function (t) {
          if (console.log("用户信息", t.data), null != t.data.discount) e = t.data.discount; else var e = 100;
          a.setData({
            userInfo: t.data,
            discount: e,
            integral: t.data.integral
          });
          if ("0" == t.data.grade){
            wx.showModal({
              title: "提示",
              content: "开卡成为会员能享受优惠买单哦~"
            }), setTimeout(function () {
              wx.redirectTo({
                url: "../my/login"
              });
            }, 1500)
          }else{
            wx.navigateTo({
              url: "md"
            });
          }
        },
      })
    },
    cz: function() {
        wx.navigateTo({
            url: "cz"
        });
    },
    rechange: function() {
        wx.navigateTo({
            url: "rechange"
        });
    },
    mflq: function(t) {
        console.log(t.currentTarget.dataset.id, t.currentTarget.dataset.lqdj, this.data.userInfo.level_type);
        var e = this, a = wx.getStorageSync("UserData").id, o = wx.getStorageSync("mdid");
        console.log(a, o), Number(e.data.userInfo.level_type) < Number(t.currentTarget.dataset.lqdj) ? wx.showModal({
            title: "提示",
            content: "您的会员等级不足以领此券哦~"
        }) : app.util.request({
            url: "entry/wxapp/AddCoupons",
            cachetime: "0",
            data: {
                user_id: a,
                coupons_id: t.currentTarget.dataset.id
            },
            success: function(t) {
                console.log(t.data), 1 == t.data ? (wx.showToast({
                    title: "领取成功"
                }), setTimeout(function() {
                    e.lqyhq(a, o);
                }, 1e3)) : wx.showToast({
                    title: "网络问题"
                });
            }
        });
    },
    tzxcx: function(t) {
        console.log(t.currentTarget.dataset.appid), wx.navigateToMiniProgram({
            appId: t.currentTarget.dataset.appid,
            success: function(t) {
                console.log(t);
            }
        });
    },
    onLoad: function(t) {
        console.log("onLoad()");
        var e = this;
        app.getUserInfo(function(t) {
            console.log(t), e.setData({
                userInfo: t
            }), e.reLoad(t.id);
        }), app.util.request({
            url: "entry/wxapp/system",
            cachetime: "0",
            success: function(t) {
                console.log(t.data), e.setData({
                    xtxx: t.data
                }), wx.setStorageSync("xtxx", t.data), qqmapsdk = new QQMapWX({
                    key: t.data.mapkey
                }), wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: t.data.link_color
                }), wx.setNavigationBarTitle({
                    title: t.data.link_name
                });
            }
        }), app.util.request({
            url: "entry/wxapp/url",
            cachetime: "0",
            success: function(t) {
                console.log(t.data), e.setData({
                    url: t.data
                }), getApp().imgurl = t.data;
            }
        }), app.util.request({
            url: "entry/wxapp/DelAllCoupons",
            cachetime: "0",
            success: function(t) {
                console.log("DelAllCoupons", t.data);
            }
        });
    },
    reLoad: function(t) {
        var e = this;
        app.util.request({
            url: "entry/wxapp/Store",
            cachetime: "0",
            success: function(a) {
                console.log("门店信息1111", a.data), e.lqyhq(t, a.data.id), wx.setStorageSync("mdid", a.data.id), 
                e.setData({
                    mdinfo: a.data
                });
            }
        });
    },
    StoreInfo: function(t) {
        var e = this, a = wx.getStorageSync("UserData").id;
        console.log(a), app.util.request({
            url: "entry/wxapp/StoreInfo",
            cachetime: "0",
            data: {
                id: t
            },
            success: function(t) {
                console.log("门店信息2222", t.data), setTimeout(function() {
                    e.lqyhq(a, t.data.id);
                }, 1e3), wx.setStorageSync("mdid", t.data.id), e.setData({
                    mdinfo: t.data
                }), wx.showToast({
                    title: "刷新成功"
                });
            }
        });
    },
    onReady: function() {},
    changeData: function() {
        console.log("changeData");
        var t = wx.getStorageSync("UserData").id;
        console.log(t);
        var e = this;
        app.util.request({
            url: "entry/wxapp/UserInfo",
            cachetime: "0",
            data: {
                user_id: t
            },
            success: function(t) {
                console.log("用户信息", t.data), e.setData({
                    userInfo: t.data
                });
            }
        });
    },
    lqyhq: function(t, e) {
        var a = this;
        app.util.request({
            url: "entry/wxapp/Coupons",
            cachetime: "0",
            data: {
                user_id: t,
                store_id: e
            },
            success: function(t) {
                console.log("优惠券信息", t.data);
                var e = a.getIdDataSet(t.data.ok);
                a.classify(t.data.all, e);
            }
        });
    },
    getIdDataSet: function(t) {
        for (var e = new Array(), a = t.length, o = 0; o < a; o++) e.push(t[o].coupons_id);
        return e;
    },
    classify: function(t, e) {
        for (var a = new Array(), o = new Array(), n = t.length, s = 0; s < n; s++) -1 === e.indexOf(t[s].id) ? o.push(t[s]) : a.push(t[s]);
        console.log(o, a), this.setData({
            unreceive: o,
            received: a
        });
    },
    onShow: function() {
        console.log("onshow()"), this.changeData();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        wx.getStorageSync("UserData").id;
        var t = wx.getStorageSync("mdid");
        this.changeData(), this.StoreInfo(t), wx.stopPullDownRefresh();
    },
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});